package com.zclever.kstarwar.controller;

import com.zclever.kstarwar.vo.Result;
import com.zclever.kstarwar.vo.User;
import com.zclever.kstarwar.websocket.WebSocketServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户控制器
 * @author zclever
 * @email 304078606@qq.com
 * @date 2019/8/21
 */
@RestController
public class UserController {

	private static final Logger logger = LogManager.getLogger(UserController.class);

    @RequestMapping("/user/list/{uid}")
    public Result<List<User>> userList(@PathVariable("uid") int uid){

        List list = new ArrayList<>();

        //从所有连接用户中取出非战斗中的用户
        WebSocketServer.getWebSocketSet().forEach(server -> {
            User user = server.getUser();
            if(user.getUid()!=uid){
                list.add(user);
            }
        });
		
		logger.info("获取在线用户列表:{}",list);

        return new Result<>(list);
    }
}
